<!DOCTYPE html>
<html lang="zh-CN">
<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  
  
  <!-- 关键字 keywords -->
  
  <meta name="keywords" content="c,指针,天赐,天赐の小站,博客">
  
  
  <title itemprop="name">C语言中的指针 | 天赐の小站</title>
  
    <link rel="shortcut icon" href="//fastly.jsdelivr.net/gh/tiancixiong/cdn@v1.0/img/custom/favicon.ico">
  
  <meta http-equiv="x-dns-prefetch-control" content="on">
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Noto+SerifMerriweather|Merriweather+Sans|Source+Code+Pro|Ubuntu:400,700|Noto+Serif+SC" media="all">
  <link rel="dns-prefetch" href="//cdn.jsdelivr.net">
  <link rel="stylesheet" id="saukra_css-css" href="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/css/style.css" type="text/css" media="all">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/css/lib.min.css" media="all">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/css/font.css" media="all">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/css/insight.css" media="all">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/css/jquery.fancybox.min.css" media="all">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/css/zoom.css" media="all">
  <link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/css/sharejs.css">
<!--   <link rel="stylesheet" id="saukra_css-css" href="https://2heng.xin/wp-content/cache/autoptimize/css/autoptimize_ad42a61f4c7d4bdd9f91afcff6b5dda5.css
" type="text/css" media="all"> -->
  <script>
  /*Initial Variables*/
  var mashiro_option = new Object();
  var mashiro_global = new Object();
  mashiro_option.NProgressON = true;
  /* 
   * 邮箱信息之类的东西可以填在这里，这些js变量基本都作用于sakura-app.js
   * 这样的设置仅是为了方便在基于PHP开发的主题中设置js变量，既然移植到了Node上，我想或许可以精简这一逻辑吧
   */
  mashiro_option.email_domain = "";
  mashiro_option.email_name = "";
  mashiro_option.cookie_version_control = "";
  mashiro_option.qzone_autocomplete = false;
  mashiro_option.site_name = "天赐の小站";
  mashiro_option.author_name = "天赐の小站";
  mashiro_option.site_url = "/";
  mashiro_option.v_appId = "VeenC2gYYyfSOq2rcCSmmruy-MdYXbMMI";
  mashiro_option.v_appKey = "F5kjyHdh4akqc7SDmfzQP8Qi";
  mashiro_option.v_notify = false;
  mashiro_option.v_verify = false;
  mashiro_option.v_placeholder = "你是我一生只会遇见一次的惊喜 ...";
  mashiro_option.v_avatar = "monsterid";
  mashiro_option.v_pageSize = "5" || 10;
  mashiro_option.mathjax = "0";
  mashiro_option.qq_api_url = "https://api.mashiro.top/qqinfo/"; 
  mashiro_option.qq_avatar_api_url = "https://api.mashiro.top/qqinfo/";

  // mashiro_option.jsdelivr_css_src = "https://cdn.jsdelivr.net/gh/moezx/cdn@3.4.5/css/lib.min.css";
  // mashiro_option.float_player_on = true;
  console.info("%c Blog %c", "background:#24272A; color:#ffffff", "", "https://blog.xiongtianci.com");
  console.info("%c GitHub %c", "background:#24272A; color:#ffffff", "", "https://github.com/tiancixiong");
  /*End of Initial Variables*/
  </script>
  <script type="text/javascript">
  var bg = "https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/5cm_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/5cm_03.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/A.Voyage.of.Chihiro_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/A.Voyage.of.Chihiro_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/A.Voyage.of.Chihiro_03.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/A.Voyage.of.Chihiro_04.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/A.Voyage.of.Chihiro_05.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/A.Voyage.of.Chihiro_06.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/A.Voyage.of.Chihiro_07.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/A.Voyage.of.Chihiro_08.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/A.Voyage.of.Chihiro_09.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/A.Voyage.of.Chihiro_10.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/A.Voyage.of.Chihiro_11.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/A.Voyage.of.Chihiro_12.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Atonement_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Atonement_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Atonement_03.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Atonement_04.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Avatar_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Before.Sunrise_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Before.Sunrise_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Before.Sunset_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/bighero_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/bighero_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/bighero_03.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Coco_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Coco_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Coco_03.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/CUG.King.of.Heroes_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Digimon_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Digimon_03.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Digimon_04.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Digimon_05.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Digimon_06.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Digimon_07.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Digimon_08.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Digimon_09.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Friends_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Friends_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Friends_03.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Friends_04.webp,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Friends_05.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Harry.Potter_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Harry.Potter_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Harry.Potter_03.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Harry.Potter_04.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Harry.Potter_05.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Hauru.no.ugoku.shiro_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Into.the.Wild_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Kaze.tachinu_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/kiki_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/kiki_03.png,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/kiki_04.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/kiminona_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/kiminona_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/kiminona_03.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/kiminona_06.png,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/kiminona_07.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/koenokatachi_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/koenokatachi_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/koenokatachi_03.png,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Kotonoha.no.Niwa_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/La.La.Land_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Le.Petit.Prince_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Le.Petit.Prince_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Mimi.wo.sumaseba_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Mr.Bean.Holiday_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Mr.Bean.Holiday_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Mr.Bean.Holiday_03.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Mr.Bean.Holiday_04.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Mr.Bean.Holiday_05.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/My.Sassy.Girl_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Paperman_01.png,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Paperman_02.png,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Piper_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Ponyo_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Ponyo_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Ponyo_03.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Reply.1988_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Reply.1988_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Reply.1988_03.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/SpiderMan.Into.the.SpiderVerse_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/SpiderMan.Into.the.SpiderVerse_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/SpiderMan.Into.the.SpiderVerse_03.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/The.Girl.Who.Leapt.Through.Time_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/The.Pianist_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/TheLonelyAndGreatGod_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Titanic_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Titanic_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Titanic_03.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/totoro1.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/WALL_E_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/WALL_E_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Zootopia_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/2049.Blade.Runner_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/2049.Blade.Runner_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/American.Dreams.in.China_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Biri.Girl_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Bohemian.Rhapsody_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Bohemian.Rhapsody_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Bohemian.Rhapsody_03.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Crouching.Tiger.Hidden.Dragon.2000_01.webp,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Detachment_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Detachment_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Gisaengchung_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Iron.Man_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Kureyon.Shinchan_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Love.Actually_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Roman.Holiday_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Roman.Holiday_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Sherlock.S01_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/The.Secret.Life.of.Walter.Mitty_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Youth.Over.Flowers.in.Africa_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Youth.Over.Flowers.in.Africa_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/My.Mister_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/One.Day_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/One.Day_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/One.Day_03.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Ready.Player.One_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Begin.Again_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Begin.Again_02.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/Freddie_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/naruto.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/SetoNaikai_01.jpg,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/yexiu_01.png,https://cdn.jsdelivr.net/gh/tiancixiong/BlogIMG@230521/theme/cover/yexiu_02_FUJISKYTREE.png".split(",");
  var bgindex = Math.floor(Math.random()*bg.length);
  if (!!window.ActiveXObject || "ActiveXObject" in window) { //is IE?
    alert('朋友，IE浏览器未适配哦~');
  }
  </script>
  <style type="text/css">
  .hljs-ln{border-collapse:collapse}.hljs-ln td{padding:0}.hljs-ln-n:before{content:attr(data-line-number)}
  </style>
  <style type="text/css">.site-top .lower nav{display:block !important;}.author-profile i,.post-like a,.post-share .show-share,.sub-text,.we-info a,span.sitename,.post-more i:hover,#pagination a:hover,.post-content a:hover,.float-content i:hover{color:#FE9600}.feature i,.download,.navigator i:hover,.links ul li:before,.ar-time i,span.ar-circle,.object,.comment .comment-reply-link,.siren-checkbox-radio:checked + .siren-checkbox-radioInput:after{background:#FE9600}::-webkit-scrollbar-thumb{background:#FE9600}.download,.navigator i:hover,.link-title,.links ul li:hover,#pagination a:hover,.comment-respond input[type='submit']:hover{border-color:#FE9600}.entry-content a:hover,.site-info a:hover,.comment h4 a,#comments-navi a.prev,#comments-navi a.next,.comment h4 a:hover,.site-top ul li a:hover,.entry-title a:hover,#archives-temp h3,span.page-numbers.current,.sorry li a:hover,.site-title a:hover,i.iconfont.js-toggle-search.iconsearch:hover,.comment-respond input[type='submit']:hover{color:#FE9600}.comments .comments-main{display:block !important;}.comments .comments-hidden{display:none !important;}background-position:center center;background-attachment:inherit;}
  </style>
</head>
</html>
<body class="page-template page-template-user page-template-page-analytics page-template-userpage-analytics-php page page-id-1297 chinese-font serif isWebKit">
  <div class="scrollbar" id="bar">
  </div>
  <a href="#" class="cd-top faa-float animated"></a>
  <section id="main-container">
    <div class="headertop filter-dot">
  <!--首页背景图下方的波浪线-->
  <!--<div id="banner_wave_1"></div>-->
  <!--<div id="banner_wave_2"></div>-->
  <figure id="centerbg" class="centerbg">
    <div class="focusinfo no-select">
      <div class="header-tou">
        <a href="/">
          <img src="https://fastly.jsdelivr.net/gh/tiancixiong/cdn@v1.0/img/custom/avatar.jpg">
        </a>
      </div>
      <div class="header-info">
        <p>You Only Live Once</p>
        <div class="top-social_v2">
          <li id="bg-pre">
            <img class="flipx" src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/img/other/next-b.svg">
          </li>
          
            
              
                <li>
                  <a href="https://github.com/tiancixiong" target="_blank" class="social-github" title="github">
                    <img src="https://fastly.jsdelivr.net/gh/tiancixiong/cdn@v1.0/img/social/github.png">
                  </a>
                </li>
              
            
              
                <li>
                  <a href="mailto:support@xiongtianci.com" target="_blank" class="social-github" title="E-mail">
                    <img src="https://fastly.jsdelivr.net/gh/tiancixiong/cdn@v1.0/img/social/email.svg">
                  </a>
                </li>
              
            
          
          <li id="bg-next">
            <img src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/img/other/next-b.svg">
          </li>
        </div>
      </div>
    </div>
  </figure>
  <div id="video-container" style="">
    <video style="object-fit: fill" id="bgvideo" class="video" video-name="" src="" width="auto" preload="auto">
    </video>
    <div id="video-btn" class="loadvideo videolive">
    </div>
    <div id="video-add">
    </div>
    <div class="video-stu">
    </div>
  </div>
  <div class="headertop-down faa-float animated" onclick="headertop_down()">
    <span>
      <i class="fa fa-chevron-down" aria-hidden="true">
      </i>
    </span>
  </div>
</div>
    <div id="page" class="site wrapper">
      <header class="site-header no-select gizle sabit" role="banner">
  <div class="site-top">
    <div class="site-branding">
      <span class="site-title">
        <span class="logolink moe-mashiro">
          <a href="/">
            <span class="sakurasono"></span>
            <span class="shironeko">天赐の小站</span>
          </a>
        </span>
      </span>
    </div>
    <div class="searchbox search-form-submit">
      <i class="iconfont js-toggle-search iconsearch icon-search">
      </i>
    </div>
    <div id="show-nav" class="showNav mobile-fit">
      <div class="line line1">
      </div>
      <div class="line line2">
      </div>
      <div class="line line3">
      </div>
    </div>
    <div class="lower-cantiner">
      <div class="lower">
        <nav class="mobile-fit-control hide">
          <ul id="menu-new" class="menu">
            
              <li>
                <a href="/">
                  <span class="faa-parent animated-hover">
                    <i class="fa  fa-fort-awesome faa-horizontal" aria-hidden="true"></i>
                    首页
                  </span>
                </a>
                
              </li>
            
              <li>
                <a href="/archives">
                  <span class="faa-parent animated-hover">
                    <i class="fa  fa-terminal faa-vertical" aria-hidden="true"></i>
                    极客
                  </span>
                </a>
                
                  <ul class="sub-menu">
                    
                      <li>
                        <a href="/categories/技术/">
                          <i class="fa fa-code" aria-hidden="true"></i>
                          技术
                        </a>
                      </li>
                    
                      <li>
                        <a href="/categories/Linux/">
                          <i class="fa fa-linux" aria-hidden="true"></i>
                          Linux
                        </a>
                      </li>
                    
                  </ul>
                
              </li>
            
              <li>
                <a href="/archives">
                  <span class="faa-parent animated-hover">
                    <i class="fa  fa-slack faa-spin" aria-hidden="true"></i>
                    爱好
                  </span>
                </a>
                
                  <ul class="sub-menu">
                    
                      <li>
                        <a href="/categories/电影/">
                          <i class="fa fa-film" aria-hidden="true"></i>
                          电影
                        </a>
                      </li>
                    
                      <li>
                        <a href="/categories/日语/">
                          <i class="fa fa-language" aria-hidden="true"></i>
                          日语
                        </a>
                      </li>
                    
                  </ul>
                
              </li>
            
              <li>
                <a href="/archives">
                  <span class="faa-parent animated-hover">
                    <i class="fa  fa-archive faa-ring" aria-hidden="true"></i>
                    归档
                  </span>
                </a>
                
                  <ul class="sub-menu">
                    
                      <li>
                        <a href="/categories/生活/">
                          <i class="fa fa-file-text-o" aria-hidden="true"></i>
                          生活
                        </a>
                      </li>
                    
                      <li>
                        <a href="/categories/随想/">
                          <i class="fa fa-commenting-o" aria-hidden="true"></i>
                          随想
                        </a>
                      </li>
                    
                      <li>
                        <a href="/categories/资源/">
                          <i class="fa fa-cloud-download" aria-hidden="true"></i>
                          资源
                        </a>
                      </li>
                    
                      <li>
                        <a href="/categories/转载/">
                          <i class="fa fa-book" aria-hidden="true"></i>
                          转载
                        </a>
                      </li>
                    
                  </ul>
                
              </li>
            
              <li>
                <a href="/comment/">
                  <span class="faa-parent animated-hover">
                    <i class="fa  fa-pencil-square-o faa-tada" aria-hidden="true"></i>
                    留言板
                  </span>
                </a>
                
              </li>
            
              <li>
                <a href="/links/">
                  <span class="faa-parent animated-hover">
                    <i class="fa  fa-link faa-shake" aria-hidden="true"></i>
                    友人帐
                  </span>
                </a>
                
              </li>
            
              <li>
                <a href="/donate/">
                  <span class="faa-parent animated-hover">
                    <i class="fa  fa-heart faa-burst" aria-hidden="true"></i>
                    赞赏
                  </span>
                </a>
                
              </li>
            
              <li>
                <a href="/">
                  <span class="faa-parent animated-hover">
                    <i class="fa  fa-leaf faa-falling" aria-hidden="true"></i>
                    关于
                  </span>
                </a>
                
                  <ul class="sub-menu">
                    
                      <li>
                        <a href="/about/">
                          <i class="fa fa-meetup" aria-hidden="true"></i>
                          我？
                        </a>
                      </li>
                    
                      <li>
                        <a href="/theme-sakura/">
                          <i class="fa iconfont icon-sakura" aria-hidden="true"></i>
                          主题
                        </a>
                      </li>
                    
                  </ul>
                
              </li>
            
              <li>
                <a href="/atom.xml">
                  <span class="faa-parent animated-hover">
                    <i class="fa  fa-rss faa-passing" aria-hidden="true"></i>
                    RSS
                  </span>
                </a>
                
              </li>
            
          </ul>
        </nav>
      </div>
    </div>
  </div>
</header>

      <link rel="stylesheet" type="text/css" href="/css/sharejs.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.4.2/tocbot.css">
<div class="pattern-center-blank"></div>

  <div class="pattern-center single-center">
    <!-- 有配图默认渲染第一张 -->
    <div class="pattern-attachment-img lazyload" style="background-image: url(https://images.unsplash.com/photo-1528164344705-47542687000d?w=540);" src="https://fastly.jsdelivr.net/gh/tiancixiong/cdn@v1.0/img/loader/orange.progress-bar-stripe-loader.svg" data-src="https://images.unsplash.com/photo-1528164344705-47542687000d?w=540">
    </div>
    <header class="pattern-header single-header">
      <h1 class="entry-title">
      C语言中的指针</h1>
      <p class="entry-census">
        <span>
          <a href="https://blog.xiongtianci.com">
            <img src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@v1.0/img/custom/avatar.jpg">
          </a>
        </span>
        <span>
          <a href="https://blog.xiongtianci.com">木浅</a>
        </span>
        <span class="bull">
        ·</span>
        2024-2-2<span class="bull">
        ·</span>
      <span id="busuanzi_value_page_pv"></span>次阅读</p>
    </header>
  </div>

<div id="content" class="site-content">
  <div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
      <article id="post-1" class="post-1 post type-post status-publish format-standard has-post-thumbnail hentry category-uncategorized">
        <div class="toc"></div>
        <!--<div class="toc-entry-content"><!-- 套嵌目录使用（主要为了支援评论）-->
        
        <div class="entry-content">
          <h1 id="什么是指针？"><a href="#什么是指针？" class="headerlink" title="什么是指针？"></a>什么是指针？</h1><p>在C语言中，<strong>指针（pointer）</strong> 是一种特殊的变量，它存储的是另一个变量的<strong>内存地址</strong>。换句话说，指针”指向”内存中的某个位置。</p>
<p>指针的本质是<strong>内存地址</strong>。通过指针，我们可以直接访问或修改内存中的数据。</p>
<p><strong>基本概念</strong>：</p>
<ul>
<li><strong>内存地址</strong>：计算机内存中每个字节都有一个唯一的地址</li>
<li><strong>指针变量</strong>：存储内存地址的变量</li>
<li><strong>间接访问</strong>：通过指针访问它所指向的值</li>
</ul>
<h1 id="指针的声明和初始化"><a href="#指针的声明和初始化" class="headerlink" title="指针的声明和初始化"></a>指针的声明和初始化</h1><p><strong>指针的声明</strong></p>
<pre><code class="c">数据类型 *指针变量名;
</code></pre>
<p>例如：</p>
<pre><code class="c">int *p;  // p 是一个指向 int 类型的指针
char *c; // c 是一个指向 char 类型的指针
</code></pre>
<p><strong>指针的初始化</strong>: 声明指针变量时, 可以赋初值. 注意指针的类型必须与其指向的对象的类型一致.</p>
<pre><code class="c">int x;
int * px = &amp;x; // 初始化
</code></pre>
<blockquote>
<p>没有初始化或赋值的指针是无效的指针，引用无效指针会带来难以预料的后果。</p>
</blockquote>
<h1 id="指针运算符"><a href="#指针运算符" class="headerlink" title="指针运算符"></a>指针运算符</h1><p>C语言提供了两个特殊的指针运算符：</p>
<ul>
<li><code>&amp;</code>（地址运算符）：返回变量的内存地址</li>
<li><code>*</code>（解引用运算符）：返回指针所指向地址的值</li>
</ul>
<h2 id="amp-（取地址运算符）"><a href="#amp-（取地址运算符）" class="headerlink" title="&amp;（取地址运算符）"></a><code>&amp;</code>（取地址运算符）</h2><p><code>&amp;</code> 用于获取变量的内存地址。</p>
<p><strong>示例：获取变量的地址</strong></p>
<pre><code class="c">#include &lt;stdio.h&gt;

int main() {
    int num = 10;
    printf(&quot;num 的值: %d\n&quot;, num);      // 输出 10
    printf(&quot;num 的地址: %p\n&quot;, &amp;num);   // 输出 num 的内存地址（如 0x7ffd...）
    return 0;
}
</code></pre>
<ul>
<li><code>&amp;num</code> 返回 <code>num</code> 的内存地址。</li>
</ul>
<h2 id="（解引用运算符）"><a href="#（解引用运算符）" class="headerlink" title="*（解引用运算符）"></a><code>*</code>（解引用运算符）</h2><p><code>*</code> 用于访问指针指向的内存地址中存储的值。</p>
<p><strong>示例 ：指针的基本使用</strong></p>
<pre><code class="c">#include &lt;stdio.h&gt;

int main() {
    int num = 10;
    int *p = &amp;num;  // p 存储 num 的地址

    printf(&quot;num 的值: %d\n&quot;, num);   // 10
    printf(&quot;p 存储的地址: %p\n&quot;, p); // 0x7ffd...（num 的地址）
    printf(&quot;*p 的值: %d\n&quot;, *p);     // 10（解引用 p，获取 num 的值）

    *p = 20;  // 通过指针修改 num 的值
    printf(&quot;修改后 num 的值: %d\n&quot;, num);  // 20

    return 0;
}
</code></pre>
<ul>
<li><code>*p</code> 表示访问 <code>p</code> 指向的变量的值（即 <code>num</code>）。</li>
<li>修改 <code>*p</code> 相当于修改 <code>num</code>。</li>
</ul>
<h1 id="指针与-const-关键字的结合"><a href="#指针与-const-关键字的结合" class="headerlink" title="指针与 const 关键字的结合"></a>指针与 <code>const</code> 关键字的结合</h1><p>指针与 <code>const</code> 关键字的结合可以形成三种不同的指针类型，它们有不同的特性和用途：</p>
<ol>
<li><strong>指向常量的指针（Pointer to Constant）</strong></li>
<li><strong>常量指针（Constant Pointer）</strong></li>
<li><strong>指向常量的常量指针（Constant Pointer to Constant）</strong></li>
</ol>
<h3 id="指向常量的指针（Pointer-to-Constant）"><a href="#指向常量的指针（Pointer-to-Constant）" class="headerlink" title="指向常量的指针（Pointer to Constant）"></a>指向常量的指针（Pointer to Constant）</h3><p><strong>定义</strong></p>
<pre><code class="c">const 数据类型 *指针名;
</code></pre>
<p>或</p>
<pre><code class="c">数据类型 const *指针名;  // 两种写法等价
</code></pre>
<p><strong>特点</strong></p>
<ul>
<li><strong>指针可以修改</strong>（可以指向不同的地址）。</li>
<li><strong>不能通过该指针修改它所指向的数据</strong>（数据是 <code>const</code> 的）。</li>
<li><strong>适用于保护数据不被修改</strong>，常用于函数参数传递。</li>
</ul>
<p><strong>示例</strong></p>
<pre><code class="c">#include &lt;stdio.h&gt;

int main() {
    int num = 10;
    const int *ptr = &amp;num;  // ptr 是指向常量的指针

    printf(&quot;*ptr = %d\n&quot;, *ptr);  // 输出 10

    // *ptr = 20;  // 错误！不能通过 ptr 修改 num
    num = 20;      // 合法：直接修改 num

    printf(&quot;*ptr = %d\n&quot;, *ptr);  // 输出 20

    int another_num = 30;
    ptr = &amp;another_num;  // 合法：指针可以指向新地址
    printf(&quot;*ptr = %d\n&quot;, *ptr);  // 输出 30

    return 0;
}
</code></pre>
<p><strong>关键点：</strong></p>
<ul>
<li><code>ptr</code> 可以重新指向不同的变量（<code>ptr = &amp;another_num</code>）。</li>
<li>但不能通过 <code>ptr</code> 修改数据（<code>*ptr = 20</code> 会报错）。</li>
</ul>
<h3 id="常量指针（Constant-Pointer）"><a href="#常量指针（Constant-Pointer）" class="headerlink" title="常量指针（Constant Pointer）"></a>常量指针（Constant Pointer）</h3><p>常量指针, 简称常指针: 指针本身的值不能修改.</p>
<p><strong>定义</strong></p>
<pre><code class="c">数据类型 *const 指针名;
</code></pre>
<p><strong>特点</strong></p>
<ul>
<li><strong>指针不能修改</strong>（始终指向同一个地址）。</li>
<li><strong>可以通过该指针修改它所指向的数据</strong>（数据不是 <code>const</code> 的）。</li>
<li><strong>适用于固定内存地址访问</strong>，如硬件寄存器操作。</li>
</ul>
<p><strong>示例</strong></p>
<pre><code class="c">#include &lt;stdio.h&gt;

int main() {
    int num = 10;
    int *const ptr = &amp;num;  // ptr 是常量指针

    printf(&quot;*ptr = %d\n&quot;, *ptr);  // 输出 10

    *ptr = 20;  // 合法：可以通过 ptr 修改 num
    printf(&quot;*ptr = %d\n&quot;, *ptr);  // 输出 20

    // ptr = &amp;another_num;  // 错误！ptr 不能指向新地址

    return 0;
}
</code></pre>
<p><strong>关键点：</strong></p>
<ul>
<li><code>ptr</code> 不能重新赋值（<code>ptr = &amp;another_num</code> 会报错）。</li>
<li>但可以通过 <code>ptr</code> 修改数据（<code>*ptr = 20</code> 合法）。</li>
</ul>
<h3 id="指向常量的常量指针（Constant-Pointer-to-Constant）"><a href="#指向常量的常量指针（Constant-Pointer-to-Constant）" class="headerlink" title="指向常量的常量指针（Constant Pointer to Constant）"></a>指向常量的常量指针（Constant Pointer to Constant）</h3><p><strong>定义</strong></p>
<pre><code class="c">const 数据类型 *const 指针名;
</code></pre>
<p><strong>特点</strong></p>
<ul>
<li><strong>指针不能修改</strong>（始终指向同一个地址）。</li>
<li><strong>也不能通过该指针修改它所指向的数据</strong>（数据是 <code>const</code> 的）。</li>
<li><strong>适用于完全只读访问</strong>，如字符串常量或硬件只读寄存器。</li>
</ul>
<p><strong>示例</strong></p>
<pre><code class="c">#include &lt;stdio.h&gt;

int main() {
    int num = 10;
    const int *const ptr = &amp;num;  // ptr 是指向常量的常量指针

    printf(&quot;*ptr = %d\n&quot;, *ptr);  // 输出 10

    // *ptr = 20;  // 错误！不能通过 ptr 修改 num
    num = 20;      // 合法：直接修改 num

    printf(&quot;*ptr = %d\n&quot;, *ptr);  // 输出 20

    // ptr = &amp;another_num;  // 错误！ptr 不能指向新地址

    return 0;
}
</code></pre>
<p><strong>关键点：</strong></p>
<ul>
<li><code>ptr</code> 既不能重新赋值（<code>ptr = &amp;another_num</code> 报错）。</li>
<li>也不能通过 <code>ptr</code> 修改数据（<code>*ptr = 20</code> 报错）。</li>
</ul>
<h3 id="实际应用场景"><a href="#实际应用场景" class="headerlink" title="实际应用场景"></a>实际应用场景</h3><h4 id="指向常量的指针（保护函数参数）"><a href="#指向常量的指针（保护函数参数）" class="headerlink" title="指向常量的指针（保护函数参数）"></a>指向常量的指针（保护函数参数）</h4><pre><code class="c">void print_array(const int *arr, int size) {
    for (int i = 0; i &lt; size; i++) {
        printf(&quot;%d &quot;, arr[i]);
        // arr[i] = 0;  // 错误！不能修改数据
    }
}
</code></pre>
<p><strong>作用：</strong> 防止函数内部意外修改外部数据。</p>
<h4 id="常量指针（硬件寄存器访问）"><a href="#常量指针（硬件寄存器访问）" class="headerlink" title="常量指针（硬件寄存器访问）"></a>常量指针（硬件寄存器访问）</h4><pre><code class="c">volatile uint32_t *const GPIO_PORT = (uint32_t *)0x40000000;
*GPIO_PORT = 0xFF;  // 合法：修改寄存器值
// GPIO_PORT = 0x50000000;  // 错误！指针不能修改
</code></pre>
<p><strong>作用：</strong> 确保指针始终指向固定的硬件地址。</p>
<h4 id="指向常量的常量指针（字符串常量）"><a href="#指向常量的常量指针（字符串常量）" class="headerlink" title="指向常量的常量指针（字符串常量）"></a>指向常量的常量指针（字符串常量）</h4><pre><code class="c">const char *const MESSAGE = &quot;Hello, World!&quot;;
printf(&quot;%s\n&quot;, MESSAGE);
// MESSAGE[0] = &#39;h&#39;;  // 错误！不能修改数据
// MESSAGE = &quot;New&quot;;   // 错误！指针不能修改
</code></pre>
<p><strong>作用：</strong> 确保字符串完全不可修改。</p>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><ul>
<li><strong><code>const int *ptr</code></strong>（指向常量的指针）：数据不可变，指针可变。</li>
<li><strong><code>int *const ptr</code></strong>（常量指针）：指针不可变，数据可变。</li>
<li><strong><code>const int *const ptr</code></strong>（指向常量的常量指针）：指针和数据都不可变。</li>
</ul>
<p><strong>核心规则：</strong></p>
<ul>
<li><code>const</code> 在 <code>*</code> 左边 → <strong>数据不可变</strong>。</li>
<li><code>const</code> 在 <code>*</code> 右边 → <strong>指针不可变</strong>。</li>
</ul>
<h1 id="指针算术运算"><a href="#指针算术运算" class="headerlink" title="指针算术运算"></a>指针算术运算</h1><p>指针算术运算（Pointer Arithmetic）是C语言中一项强大但需要谨慎使用的特性。它允许我们对指针进行加减运算，从而在内存中移动指针位置。</p>
<h2 id="基本概念"><a href="#基本概念" class="headerlink" title="基本概念"></a>基本概念</h2><p>指针算术运算主要包括以下几种操作：</p>
<ul>
<li>指针与整数的加减（<code>+</code>, <code>-</code>, <code>+=</code>, <code>-=</code>, <code>++</code>, <code>--</code>）</li>
<li>指针与指针的减法（得到的是两个指针之间的距离）</li>
<li>指针的比较（<code>&gt;</code>, <code>&lt;</code>, <code>&gt;=</code>, <code>&lt;=</code>, <code>==</code>, <code>!=</code>）</li>
</ul>
<p><strong>重要特性</strong>：</p>
<ul>
<li>指针运算的单位是<strong>所指向类型的大小</strong>，而不是字节</li>
<li>对<code>void*</code>指针不能直接进行算术运算（因为编译器不知道类型大小）</li>
</ul>
<h2 id="指针与整数的运算"><a href="#指针与整数的运算" class="headerlink" title="指针与整数的运算"></a>指针与整数的运算</h2><h3 id="基本运算规则"><a href="#基本运算规则" class="headerlink" title="基本运算规则"></a>基本运算规则</h3><pre><code class="c">int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr;  // 等价于 &amp;arr[0]

ptr = ptr + 1;   // 现在指向arr[1]
ptr += 2;        // 现在指向arr[3]
ptr--;           // 现在指向arr[2]
</code></pre>
<p><strong>内存变化</strong>（假设<code>int</code>占4字节）：</p>
<ul>
<li><code>ptr + 1</code> 实际上是 <code>ptr + 1*sizeof(int)</code> = 原地址 + 4字节</li>
<li><code>ptr + n</code> 实际上是 <code>ptr + n*sizeof(int)</code></li>
</ul>
<p>示例代码</p>
<pre><code class="c">#include &lt;stdio.h&gt;

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int *ptr = arr;

    printf(&quot;初始指针指向: %p, 值: %d\n&quot;, ptr, *ptr);

    ptr++;
    printf(&quot;ptr++ 后指向: %p, 值: %d\n&quot;, ptr, *ptr);

    ptr += 2;
    printf(&quot;ptr+=2 后指向: %p, 值: %d\n&quot;, ptr, *ptr);

    ptr--;
    printf(&quot;ptr-- 后指向: %p, 值: %d\n&quot;, ptr, *ptr);

    return 0;
}
</code></pre>
<h2 id="指针与指针的运算"><a href="#指针与指针的运算" class="headerlink" title="指针与指针的运算"></a>指针与指针的运算</h2><h3 id="指针减法"><a href="#指针减法" class="headerlink" title="指针减法"></a>指针减法</h3><p>两个相同类型的指针相减，得到的是它们之间相隔的元素个数：</p>
<pre><code class="c">int arr[5] = {0};
int *p1 = &amp;arr[0];
int *p2 = &amp;arr[3];

ptrdiff_t diff = p2 - p1;  // 结果是3，不是字节差
</code></pre>
<p><strong>注意</strong>：</p>
<ul>
<li>结果是<code>ptrdiff_t</code>类型（定义在<code>stddef.h</code>中的有符号整数类型）</li>
<li>只有指向同一数组的指针相减才有意义</li>
</ul>
<p>示例代码</p>
<pre><code class="c">#include &lt;stdio.h&gt;
#include &lt;stddef.h&gt;

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    int *start = &amp;arr[0];
    int *end = &amp;arr[4];

    ptrdiff_t distance = end - start;
    printf(&quot;两个指针之间的距离: %td 个元素\n&quot;, distance);
    printf(&quot;实际字节差: %td 字节\n&quot;, 
          (char*)end - (char*)start);

    return 0;
}
</code></pre>
<h2 id="指针比较运算"><a href="#指针比较运算" class="headerlink" title="指针比较运算"></a>指针比较运算</h2><p>可以比较两个指针的大小关系（通常用于同一数组或内存块中的指针）：</p>
<pre><code class="c">int arr[5] = {0};
int *p1 = &amp;arr[0];
int *p2 = &amp;arr[3];

if (p1 &lt; p2) {
    printf(&quot;p1在p2之前\n&quot;);
}
</code></pre>
<p><strong>注意</strong>：</p>
<ul>
<li>比较不同数组的指针结果是未定义的</li>
<li>比较NULL指针是允许的</li>
</ul>
<h2 id="不同类型指针的运算差异"><a href="#不同类型指针的运算差异" class="headerlink" title="不同类型指针的运算差异"></a>不同类型指针的运算差异</h2><p>指针运算的步长取决于指向类型的大小：</p>
<table>
<thead>
<tr>
<th style="text-align:left">数据类型</th>
<th style="text-align:left">典型大小</th>
<th style="text-align:left">ptr+1的地址增量</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">char</td>
<td style="text-align:left">1字节</td>
<td style="text-align:left">+1</td>
</tr>
<tr>
<td style="text-align:left">int</td>
<td style="text-align:left">4字节</td>
<td style="text-align:left">+4</td>
</tr>
<tr>
<td style="text-align:left">double</td>
<td style="text-align:left">8字节</td>
<td style="text-align:left">+8</td>
</tr>
<tr>
<td style="text-align:left">结构体</td>
<td style="text-align:left">不定</td>
<td style="text-align:left">+sizeof(结构体)</td>
</tr>
</tbody>
</table>
<p>示例代码</p>
<pre><code class="c">#include &lt;stdio.h&gt;

int main() {
    char char_arr[5] = {&#39;a&#39;,&#39;b&#39;,&#39;c&#39;,&#39;d&#39;,&#39;e&#39;};
    int int_arr[5] = {1,2,3,4,5};

    char *cptr = char_arr;
    int *iptr = int_arr;

    printf(&quot;char指针运算:\n&quot;);
    printf(&quot;cptr: %p, cptr+1: %p (差 %ld 字节)\n&quot;, 
          (void*)cptr, (void*)(cptr+1), (long)(cptr+1)-(long)cptr);

    printf(&quot;\nint指针运算:\n&quot;);
    printf(&quot;iptr: %p, iptr+1: %p (差 %ld 字节)\n&quot;, 
          (void*)iptr, (void*)(iptr+1), (long)(iptr+1)-(long)iptr);

    return 0;
}
</code></pre>
<h2 id="指针运算的常见应用"><a href="#指针运算的常见应用" class="headerlink" title="指针运算的常见应用"></a>指针运算的常见应用</h2><h3 id="1-数组遍历"><a href="#1-数组遍历" class="headerlink" title="(1) 数组遍历"></a>(1) 数组遍历</h3><pre><code class="c">int arr[5] = {1,2,3,4,5};
for(int *p = arr; p &lt; arr+5; p++) {
    printf(&quot;%d &quot;, *p);
}
</code></pre>
<h3 id="2-字符串处理"><a href="#2-字符串处理" class="headerlink" title="(2) 字符串处理"></a>(2) 字符串处理</h3><pre><code class="c">char str[] = &quot;Hello&quot;;
char *p = str;
while(*p != &#39;\0&#39;) {
    putchar(*p);
    p++;
}
</code></pre>
<h3 id="3-动态内存管理"><a href="#3-动态内存管理" class="headerlink" title="(3) 动态内存管理"></a>(3) 动态内存管理</h3><pre><code class="c">int *arr = malloc(5 * sizeof(int));
if(arr) {
    for(int i=0; i&lt;5; i++) {
        *(arr + i) = i * 10;  // 等价于arr[i]
    }
    free(arr);
}
</code></pre>
<h3 id="4-多维数组访问"><a href="#4-多维数组访问" class="headerlink" title="(4) 多维数组访问"></a>(4) 多维数组访问</h3><pre><code class="c">int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int *ptr = &amp;matrix[0][0];
for(int i=0; i&lt;9; i++) {
    printf(&quot;%d &quot;, *(ptr + i));
}
</code></pre>
<h2 id="特殊指针的运算"><a href="#特殊指针的运算" class="headerlink" title="特殊指针的运算"></a>特殊指针的运算</h2><h3 id="void指针的运算"><a href="#void指针的运算" class="headerlink" title="void指针的运算"></a>void指针的运算</h3><p><code>void*</code>指针不能直接进行算术运算，必须先转换为具体类型：</p>
<pre><code class="c">void *vp = malloc(100);
// vp++;  // 错误！
char *cp = (char*)vp;
cp++;    // 正确
</code></pre>
<h3 id="函数指针的运算"><a href="#函数指针的运算" class="headerlink" title="函数指针的运算"></a>函数指针的运算</h3><p>函数指针的算术运算通常是未定义行为，应避免使用。</p>
<h2 id="注意事项和常见错误"><a href="#注意事项和常见错误" class="headerlink" title="注意事项和常见错误"></a>注意事项和常见错误</h2><ol>
<li><p><strong>指针越界访问</strong></p>
<pre><code class="c">int arr[5] = {0};
int *p = arr + 5;  // 指向数组末尾之后
*p = 10;           // 未定义行为！
</code></pre>
</li>
<li><p><strong>不同类型的指针运算</strong></p>
<pre><code class="c">int i = 0;
char *cp = (char*)&amp;i;
int *ip = &amp;i;
if(cp == ip) { ... }  // 合法比较
// ptrdiff_t diff = cp - ip;  // 非法！
</code></pre>
</li>
<li><p><strong>无效指针运算</strong></p>
<pre><code class="c">int *p1, *p2;
// int sum = p1 + p2;  // 非法操作！
</code></pre>
</li>
<li><p><strong>指针运算与数组索引的等价性</strong></p>
<pre><code class="c">int arr[5];
arr[2] = 10;     // 等价于
*(arr + 2) = 10; // 这两种写法完全等价
</code></pre>
</li>
</ol>
<h2 id="高级应用：自定义内存管理"><a href="#高级应用：自定义内存管理" class="headerlink" title="高级应用：自定义内存管理"></a>高级应用：自定义内存管理</h2><p>指针运算在实现自定义内存分配器时非常有用：</p>
<pre><code class="c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#define POOL_SIZE 1024

typedef struct {
    unsigned char data[POOL_SIZE];
    unsigned char *next_free;
} MemoryPool;

void init_pool(MemoryPool *pool) {
    pool-&gt;next_free = pool-&gt;data;
}

void* pool_alloc(MemoryPool *pool, size_t size) {
    if(pool-&gt;next_free + size &gt; pool-&gt;data + POOL_SIZE) {
        return NULL; // 内存不足
    }
    void *ptr = pool-&gt;next_free;
    pool-&gt;next_free += size;
    return ptr;
}

int main() {
    MemoryPool pool;
    init_pool(&amp;pool);

    int *num = pool_alloc(&amp;pool, sizeof(int));
    *num = 42;

    double *dbl = pool_alloc(&amp;pool, sizeof(double));
    *dbl = 3.14;

    printf(&quot;Allocated values: %d, %.2f\n&quot;, *num, *dbl);

    return 0;
}
</code></pre>
<h2 id="总结-1"><a href="#总结-1" class="headerlink" title="总结"></a>总结</h2><p>指针算术运算的核心要点：</p>
<ol>
<li>指针加减整数时，步长由指向类型的大小决定</li>
<li>只有指向同一数组/内存块的指针相减才有意义</li>
<li>指针比较通常用于检查指针的相对位置</li>
<li><code>void*</code>指针需要转换后才能进行算术运算</li>
<li>使用指针运算时要特别注意边界检查，避免越界访问</li>
</ol>
<p>正确使用指针算术可以写出高效灵活的代码，但不当使用也会导致难以调试的内存错误。建议：</p>
<ul>
<li>在数组遍历时优先使用索引表示法（<code>arr[i]</code>）</li>
<li>必须使用指针运算时，添加清晰的注释</li>
<li>进行边界检查，避免越界访问</li>
<li>使用<code>const</code>修饰符保护不应被修改的数据</li>
</ul>
<h1 id="指针和数组"><a href="#指针和数组" class="headerlink" title="指针和数组"></a>指针和数组</h1><h2 id="指针与一维数组"><a href="#指针与一维数组" class="headerlink" title="指针与一维数组"></a>指针与一维数组</h2><p>数组名本质上是一个指向数组第一个元素的常量指针。在 C 语言中，由于数组元素在内存中是连续存放的，因此使用指针可以非常方便地处理数组元素。</p>
<p>引用数组元素的四种方式：</p>
<ul>
<li>数组名 + 下标;</li>
<li>数组名 + 指针运算;</li>
<li>指针 + 指针运算;</li>
<li>指针 + 下标（数组运算）.</li>
</ul>
<pre><code class="c">int arr[5] = {1, 2, 3, 4, 5};
int *p = arr;  // 等价于 int *p = &amp;arr[0]

// 以下两种方式等价
printf(&quot;arr[2] = %d\n&quot;, arr[2]);
printf(&quot;*(p+2) = %d\n&quot;, *(p+2));
</code></pre>
<pre><code class="c">#include &lt;stdio.h&gt;

int main() {
    int arr[] = {1, 2, 3, 4, 5};

    printf(&quot;数组名+下标:\n&quot;);
    for (int i = 0; i &lt; 5; i++)
        printf(&quot;%2d&quot;, arr[i]);
    printf(&quot;\n\n&quot;);

    printf(&quot;数组名+指针运算:\n&quot;);
    for (int i = 0; i &lt; 5; i++)
        printf(&quot;%2d&quot;, *(arr + i));
    printf(&quot;\n\n&quot;);

    printf(&quot;指针+指针运算:\n&quot;);
    for (int *pa = arr; pa &lt; arr + 5; pa++)
        printf(&quot;%2d&quot;, *pa);
    printf(&quot;\n\n&quot;);

    printf(&quot;指针+下标:\n&quot;);
    int *pa = arr;
    for (int i = 0; i &lt; 5; i++)
        printf(&quot;%2d&quot;, pa[i]);
    printf(&quot;\n\n&quot;);

    return 0;
}
</code></pre>
<p>数组名代表数组的首地址, 当数组名出现在表达式中时, 等效于一个常指针。</p>
<pre><code class="c">int arr[] = {0, 2, 4, 8};
int *pa = arr;  // OK, 数组名代表数组的首地址，即 pa=&amp;arr[0]
*pa = 3;        // OK，等价于 arr[0]=3
*(pa + 2) = 5;  // OK，等价于 arr[2]=5
*(arr + 2) = 5; // OK，等价于 arr[2]=5
*(pa++) = 3;    // OK，等价于 arr[0]=3; pa = pa+1;
// *(arr++) = 3;   // ERROR! a代表数组首地址，等效于常指针！
</code></pre>
<p>小结: 一维数组 <code>arr[n]</code> 与指针 <code>pa=arr</code></p>
<ul>
<li>一维数组名 <code>arr</code> 是地址常量, 数组名 <code>arr</code> 与 <code>&amp;arr[0]</code> 等价;</li>
<li><code>arr+i</code> 是 <code>arr[i]</code> 的地址, <code>arr[i]</code> 与 <code>*(arr+i)</code> 等价;</li>
<li>若指针 <code>pa</code> 存储的是数组 <code>arr</code> 的首地址, 则 <code>*(pa+i)</code> 与 <code>pa[i]</code> 等价;</li>
<li>数组元素的下标访问方式也是按地址进行的;</li>
<li>指针的值可以随时改变, 即可以指向不同的元素, 通过指针访问数组的元素更加灵活;</li>
<li>数组名等效于常量指针, 值不能改变;</li>
<li><code>pa++</code> 或 <code>++pa</code> 合法, 但 <code>arr++</code> 不合法;</li>
</ul>
<p><code>arr[i]</code> &lt;=&gt; <code>pa[i]</code> &lt;=&gt; <code>*(pa+i)</code> &lt;=&gt; <code>*(arr+i)</code></p>
<h2 id="指针与二维数组"><a href="#指针与二维数组" class="headerlink" title="指针与二维数组"></a>指针与二维数组</h2><p>在 C 语言中, 二维数组是按行存储的, 可以理解为由一维数组组成的数组, 其元素也是连续存放的.</p>
<p><img src="assets/image-20250408110127461.png" alt="image-20250408110127461"></p>
<p><strong>二维数组名的含义</strong></p>
<ul>
<li><code>A</code>：整个二维数组，类型是 <code>int[2][3]</code></li>
<li><code>A[i]</code>：第 i 行的一维数组，类型是 <code>int[3]</code></li>
<li><code>A[i][j]</code>：第 i 行第 j 列的元素</li>
</ul>
<ul>
<li>对于二维数组 <code>A</code>, 虽然 <code>A</code>、<code>A[0]</code> 都是数组首地址, 但二者指向的对象不同:<ul>
<li><code>A[0]</code> 是一维数组名, 它指向的是行数组 <code>A[0]</code> 的首元素, 即 <code>*A[0]</code> 与 <code>A[0][0]</code> 等价;</li>
<li><code>A</code> 是二维数组名, 它指向的是它的首元素, 而它的元素都是一维数组（即行数组）, 因此 <code>*A</code> 与 <code>A[0]</code> 等价. 另外, 它的指针移动单位是“行”, 所以 <code>A+i</code> 对应的是第 <code>i</code> 个行数组, 即 <code>A[i]</code></li>
</ul>
</li>
</ul>
<h3 id="指针访问二维数组"><a href="#指针访问二维数组" class="headerlink" title="指针访问二维数组"></a>指针访问二维数组</h3><pre><code class="c">int arr[3][4] = {
    {1, 2, 3, 4},
    {5, 6, 7, 8},
    {9, 10, 11, 12}
};
</code></pre>
<h4 id="行指针（数组指针）"><a href="#行指针（数组指针）" class="headerlink" title="行指针（数组指针）"></a>行指针（数组指针）</h4><pre><code class="c">int (*p)[4] = arr;  // 指向包含4个int的数组的指针
</code></pre>
<ul>
<li><code>p</code> 指向第 0 行</li>
<li><code>p+1</code> 指向第 1 行（地址增加 <code>4×sizeof(int)</code>）</li>
<li><code>*(p+i)</code> 获取第 i 行首地址</li>
<li><code>*(*(p+i)+j)</code> 访问 <code>arr[i][j]</code></li>
</ul>
<h4 id="列指针（普通指针）"><a href="#列指针（普通指针）" class="headerlink" title="列指针（普通指针）"></a>列指针（普通指针）</h4><pre><code class="c">int *p = &amp;arr[0][0];  // 或 int *p = *arr;
</code></pre>
<ul>
<li><code>p</code> 指向第一个元素</li>
<li><code>p+1</code> 指向下一个元素</li>
<li>访问 <code>arr[i][j]</code>：<code>*(p + i*4 + j)</code></li>
</ul>
<h4 id="指针数组"><a href="#指针数组" class="headerlink" title="指针数组"></a>指针数组</h4><pre><code class="c">int *p[3];  // 包含3个int指针的数组
for(int i=0; i&lt;3; i++)
    p[i] = arr[i];  // 每行首地址
</code></pre>
<ul>
<li>访问 <code>arr[i][j]</code>：<code>*(p[i] + j)</code> 或 <code>p[i][j]</code></li>
</ul>
<h3 id="常见操作示例"><a href="#常见操作示例" class="headerlink" title="常见操作示例"></a>常见操作示例</h3><h4 id="遍历二维数组"><a href="#遍历二维数组" class="headerlink" title="遍历二维数组"></a>遍历二维数组</h4><pre><code class="c">// 方法1：使用下标
for(int i=0; i&lt;3; i++)
    for(int j=0; j&lt;4; j++)
        printf(&quot;%d &quot;, arr[i][j]);

// 方法2：使用行指针
int (*p)[4] = arr;
for(int i=0; i&lt;3; i++)
    for(int j=0; j&lt;4; j++)
        printf(&quot;%d &quot;, *(*(p+i)+j));

// 方法3：使用列指针
int *q = *arr;
for(int i=0; i&lt;3*4; i++)
    printf(&quot;%d &quot;, *(q+i));
</code></pre>
<h4 id="动态分配二维数组"><a href="#动态分配二维数组" class="headerlink" title="动态分配二维数组"></a>动态分配二维数组</h4><pre><code class="c">// 方法1：连续分配
int **arr = (int **)malloc(rows * sizeof(int *));
arr[0] = (int *)malloc(rows * cols * sizeof(int));
for(int i=1; i&lt;rows; i++)
    arr[i] = arr[0] + i*cols;

// 方法2：非连续分配
int **arr = (int **)malloc(rows * sizeof(int *));
for(int i=0; i&lt;rows; i++)
    arr[i] = (int *)malloc(cols * sizeof(int));
</code></pre>
<h3 id="指针与二维数组的关系"><a href="#指针与二维数组的关系" class="headerlink" title="指针与二维数组的关系"></a>指针与二维数组的关系</h3><table>
<thead>
<tr>
<th style="text-align:left">表达式</th>
<th style="text-align:left">类型</th>
<th style="text-align:left">含义</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left"><code>arr</code></td>
<td style="text-align:left"><code>int[3][4]</code></td>
<td style="text-align:left">整个二维数组</td>
</tr>
<tr>
<td style="text-align:left"><code>arr+i</code></td>
<td style="text-align:left"><code>int(*)[4]</code></td>
<td style="text-align:left">指向第i行的指针</td>
</tr>
<tr>
<td style="text-align:left"><code>*(arr+i)</code></td>
<td style="text-align:left"><code>int*</code></td>
<td style="text-align:left">第i行首元素地址</td>
</tr>
<tr>
<td style="text-align:left"><code>*(arr+i)+j</code></td>
<td style="text-align:left"><code>int*</code></td>
<td style="text-align:left">第i行第j列元素地址</td>
</tr>
<tr>
<td style="text-align:left"><code>*(*(arr+i)+j)</code></td>
<td style="text-align:left"><code>int</code></td>
<td style="text-align:left">第i行第j列元素值</td>
</tr>
<tr>
<td style="text-align:left"><code>arr[i][j]</code></td>
<td style="text-align:left"><code>int</code></td>
<td style="text-align:left">第i行第j列元素值</td>
</tr>
</tbody>
</table>
<h3 id="注意事项"><a href="#注意事项" class="headerlink" title="注意事项"></a>注意事项</h3><ol>
<li><p><strong>类型匹配</strong>：行指针 <code>int(*)[N]</code> 与普通指针 <code>int*</code> 类型不同</p>
</li>
<li><p><strong>指针运算</strong>：<code>arr+1</code> 移动一行的大小，<code>*arr+1</code> 移动一个元素的大小</p>
</li>
<li><p><strong>作为函数参数</strong>：</p>
<ul>
<li><code>void func(int arr[][4], int rows)</code></li>
<li><code>void func(int (*arr)[4], int rows)</code></li>
</ul>
</li>
<li><p><strong>动态分配内存</strong>后要记得释放：</p>
<pre><code class="c">// 对于连续分配
free(arr[0]);
free(arr);

// 对于非连续分配
for(int i=0; i&lt;rows; i++)
    free(arr[i]);
free(arr);
</code></pre>
</li>
</ol>
<h1 id="指针数组和数组指针"><a href="#指针数组和数组指针" class="headerlink" title="指针数组和数组指针"></a>指针数组和数组指针</h1><p><strong>指针数组</strong>：是一个数组，其元素都是指针。</p>
<pre><code class="c">int *ptr[3];  // 包含3个int指针的数组
</code></pre>
<p><strong>数组指针</strong>：是一个指针，指向一个数组。</p>
<pre><code class="c">int (*ptr)[3];  // 指向包含3个int的数组的指针
</code></pre>
<h1 id="多级指针"><a href="#多级指针" class="headerlink" title="多级指针"></a>多级指针</h1><p>指针可以指向另一个指针，形成多级指针。</p>
<pre><code class="c">int var = 10;
int *p = &amp;var;
int **pp = &amp;p;  // 指向指针的指针

printf(&quot;var = %d\n&quot;, var);    // 10
printf(&quot;*p = %d\n&quot;, *p);      // 10
printf(&quot;**pp = %d\n&quot;, **pp);  // 10
</code></pre>
<h1 id="指针与函数"><a href="#指针与函数" class="headerlink" title="指针与函数"></a>指针与函数</h1><h2 id="指针函数"><a href="#指针函数" class="headerlink" title="指针函数"></a>指针函数</h2><p>指针函数是指<strong>返回值为指针类型</strong>的函数，其本质是一个函数，只是它返回的是一个地址（指针）。</p>
<p><strong>语法</strong></p>
<pre><code class="c">返回类型 *函数名(参数列表);
</code></pre>
<p><strong>示例</strong></p>
<pre><code class="c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

// 指针函数：返回int指针
int* createIntArray(int size) {
    int *arr = (int*)malloc(size * sizeof(int));
    if(arr == NULL) {
        return NULL;
    }
    for(int i = 0; i &lt; size; i++) {
        arr[i] = i + 1;
    }
    return arr;  // 返回动态分配的数组指针
}

int main() {
    int *myArray = createIntArray(5);
    if(myArray != NULL) {
        for(int i = 0; i &lt; 5; i++) {
            printf(&quot;%d &quot;, myArray[i]);
        }
        free(myArray);  // 记得释放内存
    }
    return 0;
}
</code></pre>
<h2 id="函数指针"><a href="#函数指针" class="headerlink" title="函数指针"></a>函数指针</h2><p>函数指针是指<strong>指向函数的指针变量</strong>，它存储的是函数的入口地址，可以通过该指针调用函数。</p>
<p><strong>语法</strong></p>
<pre><code class="c">返回类型 (*指针变量名)(参数类型列表);
</code></pre>
<p><strong>示例</strong></p>
<pre><code class="c">#include &lt;stdio.h&gt;

// 普通函数
int add(int a, int b) {
    return a + b;
}

int subtract(int a, int b) {
    return a - b;
}

int main() {
    // 声明函数指针
    int (*operation)(int, int);

    // 指向add函数
    operation = add;
    printf(&quot;10 + 5 = %d\n&quot;, operation(10, 5));

    // 指向subtract函数
    operation = subtract;
    printf(&quot;10 - 5 = %d\n&quot;, operation(10, 5));

    return 0;
}
</code></pre>
<p><strong>函数指针数组</strong></p>
<pre><code class="c">#include &lt;stdio.h&gt;

double add(double a, double b) { return a + b; }
double sub(double a, double b) { return a - b; }
double mul(double a, double b) { return a * b; }
double div(double a, double b) { return a / b; }

int main() {
    // 函数指针数组
    double (*ops[4])(double, double) = {add, sub, mul, div};
    char *op_names[] = {&quot;加法&quot;, &quot;减法&quot;, &quot;乘法&quot;, &quot;除法&quot;};

    double x = 10.0, y = 5.0;
    for(int i = 0; i &lt; 4; i++) {
        printf(&quot;%s: %.2f %c %.2f = %.2f\n&quot;, 
              op_names[i], x, 
              i == 0 ? &#39;+&#39; : i == 1 ? &#39;-&#39; : i == 2 ? &#39;*&#39; : &#39;/&#39;,
              y, ops[i](x, y));
    }

    return 0;
}
</code></pre>
<h2 id="区别"><a href="#区别" class="headerlink" title="区别"></a>区别</h2><table>
<thead>
<tr>
<th style="text-align:left">特性</th>
<th style="text-align:left">指针函数</th>
<th style="text-align:left">函数指针</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">本质</td>
<td style="text-align:left">是一个函数</td>
<td style="text-align:left">是一个指针变量</td>
</tr>
<tr>
<td style="text-align:left">声明</td>
<td style="text-align:left"><code>int* func()</code></td>
<td style="text-align:left"><code>int (*func)()</code></td>
</tr>
<tr>
<td style="text-align:left">返回值</td>
<td style="text-align:left">返回指针</td>
<td style="text-align:left">指向函数的地址</td>
</tr>
<tr>
<td style="text-align:left">调用方式</td>
<td style="text-align:left">直接调用：<code>func()</code></td>
<td style="text-align:left">通过指针调用：<code>func_ptr()</code></td>
</tr>
<tr>
<td style="text-align:left">主要用途</td>
<td style="text-align:left">返回动态分配的内存或对象指针</td>
<td style="text-align:left">实现回调、多态、插件架构等</td>
</tr>
<tr>
<td style="text-align:left">内存管理</td>
<td style="text-align:left">通常需要调用者管理返回的内存</td>
<td style="text-align:left">不需要特殊的内存管理</td>
</tr>
</tbody>
</table>
<h1 id="void指针"><a href="#void指针" class="headerlink" title="void指针"></a>void指针</h1><p>void指针是一种通用指针，可以指向任何数据类型。在使用前需要强制类型转换。</p>
<pre><code class="c">void * 指针名
</code></pre>
<ul>
<li>void 类型的指针可以存储任何类型的对象的地址;</li>
<li>不允许直接使用 void 指针访问其目标对象;</li>
<li>必须通过显式类型转换, 才可以访问 void 类型指针的目标对象.</li>
</ul>
<pre><code class="c">int a = 10;
float b = 3.14;
void *p;

p = &amp;a;
printf(&quot;a = %d\n&quot;, *(int *)p);

p = &amp;b;
printf(&quot;b = %f\n&quot;, *(float *)p);
</code></pre>
<p>可以存储任何类型的地址。<code>void *</code> 是一种“无类型指针”，可以指向 <code>int</code>、<code>char</code>、<code>float</code> 等任意类型的数据：</p>
<pre><code class="c">int num = 10;
float f = 3.14;
char c = &#39;A&#39;;

void *p1 = &amp;num;  // 存储 int 的地址
void *p2 = &amp;f;    // 存储 float 的地址
void *p3 = &amp;c;    // 存储 char 的地址
</code></pre>
<p>不能直接解引用。<code>void *</code> <strong>不知道它指向的数据类型</strong>，所以不能直接用 <code>*</code> 访问数据：</p>
<pre><code class="c">void *p = &amp;num;
printf(&quot;%d&quot;, *p);  // 错误！不能直接解引用 void 指针
</code></pre>
<p>必须强制类型转换后才能访问。要访问 <code>void *</code> 指向的数据，必须先转换为具体类型的指针：</p>
<pre><code class="c">void *p = &amp;num;
int *int_ptr = (int *)p;  // 转换为 int *
printf(&quot;%d&quot;, *int_ptr);   // 正确：输出 10
</code></pre>
<h1 id="动态内存分配"><a href="#动态内存分配" class="headerlink" title="动态内存分配"></a>动态内存分配</h1><p>指针常用于动态内存分配：</p>
<ul>
<li><code>malloc()</code>: 分配内存</li>
<li><code>calloc()</code>: 分配并初始化内存</li>
<li><code>realloc()</code>: 重新分配内存</li>
<li><code>free()</code>: 释放内存</li>
</ul>
<pre><code class="c">int *arr = (int *)malloc(5 * sizeof(int));  // 分配5个int的空间
if (arr != NULL) {
    for (int i = 0; i &lt; 5; i++) {
        arr[i] = i + 1;
    }
    free(arr);  // 释放内存
}
</code></pre>
<h1 id="常见指针错误"><a href="#常见指针错误" class="headerlink" title="常见指针错误"></a>常见指针错误</h1><ol>
<li><strong>未初始化的指针</strong>：使用前未初始化</li>
<li><strong>野指针</strong>：指针指向已释放的内存</li>
<li><strong>内存泄漏</strong>：分配的内存未释放</li>
<li><strong>指针越界</strong>：访问超出分配范围的内存</li>
<li><strong>空指针解引用</strong>：解引用NULL指针</li>
</ol>
<h1 id="指针的应用场景"><a href="#指针的应用场景" class="headerlink" title="指针的应用场景"></a>指针的应用场景</h1><ol>
<li>动态内存管理</li>
<li>实现复杂数据结构（链表、树、图等）</li>
<li>函数参数传递（实现引用传递）</li>
<li>函数回调</li>
<li>数组和字符串操作</li>
<li>系统编程和硬件访问</li>
</ol>
<h1 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h1><h2 id="指针的常见应用"><a href="#指针的常见应用" class="headerlink" title="指针的常见应用"></a>指针的常见应用</h2><h3 id="指针作为函数参数（按引用传递）"><a href="#指针作为函数参数（按引用传递）" class="headerlink" title="指针作为函数参数（按引用传递）"></a>指针作为函数参数（按引用传递）</h3><p>C语言默认是<strong>值传递</strong>，但可以通过指针实现<strong>引用传递</strong>，从而在函数内部修改外部变量。</p>
<p><strong>示例：交换两个变量的值</strong></p>
<pre><code class="c">#include &lt;stdio.h&gt;

// 错误示例：值传递（无法交换）
void swap_wrong(int a, int b) {
    int temp = a;
    a = b;
    b = temp;
}

// 正确示例：指针传递（可以交换）
void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main() {
    int x = 10, y = 20;

    swap_wrong(x, y);
    printf(&quot;x=%d, y=%d\n&quot;, x, y);  // x=10, y=20（未交换）

    swap(&amp;x, &amp;y);  // 传递 x 和 y 的地址
    printf(&quot;x=%d, y=%d\n&quot;, x, y);  // x=20, y=10（成功交换）

    return 0;
}
</code></pre>
<ul>
<li><code>swap(&amp;x, &amp;y)</code> 传递的是 <code>x</code> 和 <code>y</code> 的地址，函数内部通过 <code>*a</code> 和 <code>*b</code> 直接修改它们的值。</li>
</ul>
<h3 id="指针与数组"><a href="#指针与数组" class="headerlink" title="指针与数组"></a>指针与数组</h3><p>数组名本质上是一个<strong>指向数组首元素的指针</strong>。</p>
<p><strong>示例：指针遍历数组</strong></p>
<pre><code class="c">#include &lt;stdio.h&gt;

int main() {
    int arr[5] = {1, 2, 3, 4, 5};
    int *p = arr;  // p 指向 arr[0]

    for (int i = 0; i &lt; 5; i++) {
        printf(&quot;arr[%d] = %d\n&quot;, i, *(p + i));  // 等价于 arr[i]
    }

    return 0;
}
</code></pre>
<ul>
<li><code>*(p + i)</code> 等价于 <code>arr[i]</code>，因为指针运算会自动计算偏移量。</li>
</ul>
<h3 id="动态内存分配（malloc-和-free）"><a href="#动态内存分配（malloc-和-free）" class="headerlink" title="动态内存分配（malloc 和 free）"></a>动态内存分配（<code>malloc</code> 和 <code>free</code>）</h3><p>指针可以指向动态分配的内存，用于灵活管理数据。</p>
<p><strong>示例：动态创建数组</strong></p>
<pre><code class="c">#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;  // 提供 malloc 和 free

int main() {
    int n = 5;
    int *arr = (int *)malloc(n * sizeof(int));  // 动态分配 5 个 int 的内存

    if (arr == NULL) {
        printf(&quot;内存分配失败！\n&quot;);
        return 1;
    }

    for (int i = 0; i &lt; n; i++) {
        arr[i] = i + 1;  // 等价于 *(arr + i) = i + 1
    }

    for (int i = 0; i &lt; n; i++) {
        printf(&quot;%d &quot;, arr[i]);  // 输出 1 2 3 4 5
    }

    free(arr);  // 释放内存
    return 0;
}
</code></pre>
<ul>
<li><code>malloc</code> 动态分配内存，返回 <code>void*</code>，需要强制类型转换。</li>
<li><code>free</code> 释放内存，防止内存泄漏。</li>
</ul>
<h2 id="常见错误"><a href="#常见错误" class="headerlink" title="常见错误"></a>常见错误</h2><h3 id="未初始化的指针（野指针）"><a href="#未初始化的指针（野指针）" class="headerlink" title="未初始化的指针（野指针）"></a>未初始化的指针（野指针）</h3><pre><code class="c">int *p;  // 未初始化
*p = 10; // 错误！p 指向未知内存，可能导致程序崩溃
</code></pre>
<p><strong>修正方法：</strong></p>
<pre><code class="c">int num = 10;
int *p = &amp;num;  // 正确：p 指向 num
*p = 20;        // 合法
</code></pre>
<h3 id="空指针解引用"><a href="#空指针解引用" class="headerlink" title="空指针解引用"></a>空指针解引用</h3><pre><code class="c">int *p = NULL;  // p 是空指针
printf(&quot;%d&quot;, *p);  // 错误！解引用空指针会导致崩溃
</code></pre>
<p><strong>修正方法：</strong></p>
<pre><code class="c">if (p != NULL) {
    printf(&quot;%d&quot;, *p);
}
</code></pre>
<h2 id="总结-2"><a href="#总结-2" class="headerlink" title="总结"></a>总结</h2><table>
<thead>
<tr>
<th style="text-align:left">概念</th>
<th style="text-align:left">运算符</th>
<th style="text-align:left">作用</th>
<th style="text-align:left">示例</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left"><strong>取地址</strong></td>
<td style="text-align:left"><code>&amp;</code></td>
<td style="text-align:left">获取变量的内存地址</td>
<td style="text-align:left"><code>int *p = #</code></td>
</tr>
<tr>
<td style="text-align:left"><strong>解引用</strong></td>
<td style="text-align:left"><code>*</code></td>
<td style="text-align:left">访问指针指向的值</td>
<td style="text-align:left"><code>int val = *p;</code></td>
</tr>
<tr>
<td style="text-align:left"><strong>指针声明</strong></td>
<td style="text-align:left"><code>*</code></td>
<td style="text-align:left">定义指针变量</td>
<td style="text-align:left"><code>int *p;</code></td>
</tr>
<tr>
<td style="text-align:left"><strong>动态内存</strong></td>
<td style="text-align:left"><code>malloc</code> / <code>free</code></td>
<td style="text-align:left">分配/释放堆内存</td>
<td style="text-align:left"><code>int *arr = malloc(5 * sizeof(int));</code></td>
</tr>
</tbody>
</table>
<ul>
<li><strong>指针的核心是内存地址</strong>，<code>&amp;</code> 取地址，<code>*</code> 解引用。</li>
<li><strong>指针可用于函数传参、数组访问、动态内存管理</strong>。</li>
<li><strong>避免野指针和空指针解引用</strong>，否则会导致程序崩溃。</li>
</ul>

        </div>
        <!-- .entry-content -->
        <div class="single-reward">
          <div class="reward-open">赏<div class="reward-main">
              <ul class="reward-row">
                <li class="alipay-code"><img src="https://fastly.jsdelivr.net/gh/tiancixiong/cdn@v1.0/img/custom/donate/AliPayQR.jpg"></li>
                <li class="wechat-code"><img src="https://fastly.jsdelivr.net/gh/tiancixiong/cdn@v1.0/img/custom/donate/WeChanQR.png"></li>
              </ul>
            </div>
          </div>
        </div>
        <div style="text-align:center; width: 100%" class="social-share share-mobile" data-disabled="diandian, tencent"></div>
        <footer class="post-footer">
          <div class="post-lincenses"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh" target="_blank" rel="nofollow"><i class="fa fa-creative-commons" aria-hidden="true"></i> 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议</a></div>
          <div class="post-tags">
          </div>
          <div class="post-share">
            <div class="social-share sharehidden share-component"></div>
            <i class="iconfont show-share icon-forward"></i>
          </div>
        </footer><!-- .entry-footer -->
      </article>
      <!-- #post-## -->
      <!--<div class="toc" style="background: none;"></div>-->
      <section class="post-squares nextprev">
        
          
            <div class="post-nepre half previous">
          
            <a href="/2024/03/01/分布式ID的几种生成方式/" rel="prev">
              <div class="background">
                <img class="lazyload" src="https://fastly.jsdelivr.net/gh/tiancixiong/cdn@v1.0/img/loader/orange.progress-bar-stripe-loader.svg" data-src="https://images.unsplash.com/photo-1545943745-c39c046b282d?w=540" style="width: 100%; height: 100%; object-fit: cover; pointer-events: none;" onerror="imgError(this,3)" src="https://images.unsplash.com/photo-1545943745-c39c046b282d?w=540">
              </div>
              <span class="label">
              Previous Post</span>
              <div class="info">
                <h3>
                分布式ID的几种生成方式</h3>
                <hr>
              </div>
            </a>
          </div>
        
        
          
            <div class="post-nepre half next">
          
            <a href="/2024/02/01/关于Windows的一些设置/" rel="next">
              <div class="background">
                <img class="lazyload" src="https://fastly.jsdelivr.net/gh/tiancixiong/cdn@v1.0/img/loader/orange.progress-bar-stripe-loader.svg" data-src="https://images.unsplash.com/photo-1474123685650-afa16479e04b?w=540" style="width: 100%; height: 100%; object-fit: cover; pointer-events: none;" onerror="imgError(this,3)" src="https://images.unsplash.com/photo-1474123685650-afa16479e04b?w=540">
              </div>
              <span class="label">
              Next Post</span>
              <div class="info">
                <h3>
                关于Windows的一些设置</h3>
                <hr>
              </div>
            </a>
          </div>
        
      </section>
      <!-- 评论插件valine -->

<div id="vcomments"></div>
<script>
  window.onload = function(){
      var valine = new Valine();
      valine.init({
        el: '#vcomments',
        appId: "VeenC2gYYyfSOq2rcCSmmruy-MdYXbMMI",
        appKey: "F5kjyHdh4akqc7SDmfzQP8Qi",
		notify: false,
		verify: false,
        path: window.location.pathname,
        placeholder: "你是我一生只会遇见一次的惊喜 ...",
		avatar: "monsterid",
		//meta: [nick,mail,link],
		visitor: true,
        recordIP: "true",
		pageSize: "5" || 10,
		serverURLs: "https://VeenC2gY.api.lncldglobal.com"
      });
	//增加以下六行代码去除 power by valine
    var infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0){
      infoEle.childNodes.forEach(function(item) {
        item.parentNode.removeChild(item);
      });
    }
  }
</script>

      <section class="author-profile">
        <div class="info" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
          <a href="https://blog.xiongtianci.com" class="profile gravatar"><img src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@v1.0/img/custom/avatar.jpg" itemprop="image" alt="木浅" height="70" width="70"></a>
          <div class="meta">
            <span class="title">Author</span>
            <h3 itemprop="name">
            <a href="https://blog.xiongtianci.com" itemprop="url" rel="author">木浅</a>
            </h3>
          </div>
        </div>
        <hr>
        <p><i class="iconfont icon-write"></i>YOLO</p>
      </section>
    </main><!-- #main -->
  </div><!-- #primary -->
</div>


    </div>    
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="Type something..."/>
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: 'Posts',
            // PAGES: 'Pages',
            CATEGORIES: 'Categories',
            TAGS: 'Tags',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
    <!-- <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2025 天赐<br>
      powered_by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer> -->
<footer id="colophon" class="site-footer" role="contentinfo">
	<div class="site-info">
		<div class="footertext">
			<div class="img-preload">
				<img src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@1.0/img/other/wordpress-rotating-ball-o.svg">
				<img src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@1.0/img/other/disqus-preloader.svg">
			</div>
			<p class="foo-logo"
				style="background-image: url('https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/img/custom/sakura.svg');">
			</p>
			<div
				style="background-image:linear-gradient(90deg,#6dba82 0,#07b39b 15%,#1098ad 30%,#5073b8 44%,#a166ab 58%,#ef4e7b 72%,#f37055 86%,#f79533 100%);background-size:cover;-webkit-background-clip:text;-webkit-text-fill-color:transparent;user-select:none;margin-top:0.7em;">
				<i>Tip: 本站电脑访问体验更佳耶&nbsp;<i class="fa fa-angellist" style="display:inline"></i></i>
			</div>
			<!-- color: #666666; -->
			<div style="color: #b9b9b9;">&copy;
				2018-2025&nbsp;天赐&nbsp;
			</div>
		</div>
		<!-- 访客统计 -->
		<!--<div id="busuanzi_container"></div>-->
		<!-- 已运行的时间-容器 -->
		<div id="days"></div>
		<div class="footer-device">
			<p id="footer-sponsor">
				<!-- <a href="https://pages.github.com/" target="_blank" alt="GitHub Pages" rel="nofollow"><img
						src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@1.0/img/logo/GitHub.svg" alt="GitHub Pages"
						style="height: 2em; max-height: 2em;" title=""></a>&nbsp;&nbsp;&nbsp; -->
				<!-- <a href="https://coding.net/pages" target="_blank" alt="Coding Pages" rel="nofollow"><img
						src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@1.0/img/logo/Coding.png" alt="Coding Pages"
						style="height: 1.3em; max-height: 1.3em;padding-bottom: 0px;margin-bottom: 0.35em;"
						title=""></a>&nbsp;&nbsp;&nbsp; -->
				<a href="https://www.jsdelivr.com/" target="_blank" alt="Free CDN" rel="nofollow"><img
						src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/img/logo/JsDelivr_Logo.svg" alt="jsDelivr"
						style="height: 2em; max-height: 2em;padding-bottom: 0px;"></a>&nbsp;&nbsp;&nbsp;<a
					href="https://www.google.com/intl/ja/analytics/" target="_blank" alt="Analytics solution"
					rel="nofollow"><img src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/img/logo/Google.svg"
						alt="Google Analytics" style="height: 1.7em; max-height: 1.7em;padding-bottom: 0px;"></a>
			</p>
		</div>
	</div>
	<!-- .site-info -->
</footer>

<!-- <script src="/js/tocbot.js"></script> -->
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/js/lib.min.js"></script>
<script src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/js/vendor/clipboard.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/js/InsightSearch.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/js/jquery.fancybox.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/js/zoom.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/js/sakura-app.js"></script>
<!--浏览器搞笑标题-->
<!--<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/js/title_change.js"></script>-->
<script type="text/javascript" src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/js/volantis.min.js"></script>
<!-- Valine -->
<!--<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>-->
<script src='//unpkg.com/valine/dist/Valine.min.js'></script>
<!--<script src='//unpkg.com/valine@1.3.4/dist/Valine.min.js'></script>-->
<!--<script src="//unpkg.com/valine@1.3.7/dist/Valine.min.js"></script>-->
<!-- 不蒜子 网页计数器 -->
<script src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.4.2/tocbot.min.js"></script> -->
<script type="text/javascript">
	/* <![CDATA[ */
	if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
		var Poi = {
			"pjax": "1",
			"movies": {
				"url": "https://cdn.jsdelivr.net/gh/tiancixiong/cdn@v3.0",
				"name": "/video/Unbroken.mp4",
				"live": "close"
			},
			"windowheight": "fixed",
			"codelamp": "close",
			"ajaxurl": "",
			"order": "asc",
			"formpostion": "bottom"
		};
	} else {
		var Poi = {
			"pjax": "1",
			"movies": {
				"url": "https://cdn.jsdelivr.net/gh/tiancixiong/cdn@v3.0",
				"name": "/video/Unbroken.mp4",
				"live": "open"
			},
			"windowheight": "auto",
			"codelamp": "close",
			"ajaxurl": "",
			"order": "asc",
			"formpostion": "bottom"
		};
	}
		/* ]]> */
</script>
<script>
	$(document).ready(function () {
		if ($(".toc").length > 0 && document.body.clientWidth > 1200) {
			if ($(".pattern-center").length > 0) { //有图的情况
				tocbot.init({
					// Where to render the table of contents.
					tocSelector: '.toc', // 放置目录的容器
					// Where to grab the headings to build the table of contents.
					contentSelector: '.entry-content', // 正文内容所在
					// Which headings to grab inside of the contentSelector element.
					scrollSmooth: true,
					headingSelector: 'h1, h2, h3, h4, h5', // 需要索引的标题级别
					headingsOffset: -400,
					scrollSmoothOffset: -85
				});
			} else {
				tocbot.init({
					// Where to render the table of contents.
					tocSelector: '.toc', // 放置目录的容器
					// Where to grab the headings to build the table of contents.
					contentSelector: '.entry-content', // 正文内容所在
					// Which headings to grab inside of the contentSelector element.
					scrollSmooth: true,
					headingSelector: 'h1, h2, h3, h4, h5', // 需要索引的标题级别
					headingsOffset: -85,
					scrollSmoothOffset: -85
				});
			}
			var offsetTop = $('.toc').offset().top - 95;
			window.onscroll = function () {
				var scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
				if (scrollTop >= offsetTop) {
					$('.toc').addClass('toc-fixed');
				} else {
					$('.toc').removeClass('toc-fixed');
				}
			}
		}
	});
</script>
<!-- 已运行的时间-js -->
<script>
	function show_date_time() {
		window.setTimeout("show_date_time()", 1000);
		// 获得建站时间
		var time = "28/11/2018 15:36:06";
		time = time.replace(/\//g, ':').replace(' ', ':');
		time = time.split(':');
		BirthDay = new Date(time[2], (time[1] - 1), time[0], time[3], time[4], time[5]);

		today = new Date();
		timeold = (today.getTime() - BirthDay.getTime());
		sectimeold = timeold / 1000
		secondsold = Math.floor(sectimeold);
		msPerDay = 24 * 60 * 60 * 1000
		msPerYear = 365 * 24 * 60 * 60 * 1000
		e_yearsold = timeold / msPerYear
		yearsold = Math.floor(e_yearsold);
		//e_daysold = timeold / msPerDay
		e_daysold = timeold/msPerDay - yearsold*365;
		daysold = Math.floor(e_daysold);
		e_hrsold = (e_daysold - daysold) * 24;
		hrsold = setzero(Math.floor(e_hrsold));
		e_minsold = (e_hrsold - hrsold) * 60;
		minsold = setzero(Math.floor((e_hrsold - hrsold) * 60));
		seconds = setzero(Math.floor((e_minsold - minsold) * 60));
		document.getElementById('days').innerHTML = "已在风雨中运行 " + yearsold + " 年 " + daysold + " 天 " + hrsold + " 小时 " + minsold + " 分 " + seconds + " 秒";
	}

	function setzero(i) {
		if (i < 10) {
			i = "0" + i
		};
		return i;
	}
	show_date_time();
</script>
<!-- 访客统计-js -->
<!--<script>
		if(true){
			document.getElementById('busuanzi_container').innerHTML="<span id=\"busuanzi_container_site_pv\">本站总访问量 <span id=\"busuanzi_value_site_pv\"></span> 次</span><span class=\"post-meta-divider\"> | </span><span id=\"busuanzi_container_site_uv\">本站访客数 <span id=\"busuanzi_value_site_uv\"></span> 人</span>";
		}else{
			document.getElementById('busuanzi_container').hidden;
		}
	</script>-->
<!-- 百度统计自动推送 -->
<!--<script>
	(function(){
		var bp = document.createElement('script');
		var curProtocol = window.location.protocol.split(':')[0];
		if (curProtocol === 'https') {
			bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
		}
		else {
			bp.src = 'http://push.zhanzhang.baidu.com/push.js';
		}
		var scriptList = document.getElementsByTagName("script");
		var s = scriptList[0];
		s.parentNode.insertBefore(bp, s);
	})();
	</script>
	-->
    <div class="openNav no-select" style="height: 50px;">
      <div class="iconflat no-select" style="width: 50px; height: 50px;">
        <div class="icon"></div>
      </div>
      <div class="site-branding search-form-submit">
        <i class="iconfont js-toggle-search iconsearch icon-search"></i>
      </div>
    </div>
  </section>
  
  <!-- 实现换肤功能 -->
  <div class="skin-menu no-select" id="mainskin"  style="position: fixed">
    <div class="theme-controls row-container">
        <ul class="menu-list">
            <li id="white-bg"><i class="fa fa-television" aria-hidden="true"></i></li>
            <li id="sakura-bg"> <i class="iconfont icon-sakura"></i></li>
            <li id="gribs-bg"> <i class="fa fa-slack" aria-hidden="true"></i></li>
            <li id="KAdots-bg"> <i class="iconfont icon-dots"></i></li>
            <li id="totem-bg"> <i class="fa fa-optin-monster" aria-hidden="true"></i></li>
            <li id="pixiv-bg"> <i class="iconfont icon-pixiv"></i></li>
            <li id="bing-bg"> <i class="iconfont icon-bing"></i></li>
            <li id="dark-bg"> <i class="fa fa-moon-o" aria-hidden="true"></i></li>
        </ul>
    </div>
</div>

<canvas id="night-mode-cover"></canvas>
<style>
#mainskin{
    position: fixed;
}
</style> 
  <div class="changeSkin-gear no-select">
    <div class="keys" id="setbtn"> <span id="open-skinMenu"> 切换主题 | SCHEME TOOL &nbsp;
	<i class="iconfont icon-gear inline-block rotating"></i> </span></div>
</div>
  
  <div id="mo-nav" class="">
  <div class="m-avatar">
    <img src="https://fastly.jsdelivr.net/gh/tiancixiong/cdn@v1.0/img/custom/avatar.jpg">
  </div>
  <p style="text-align: center; color: #333; font-weight: 900; font-family: 'Ubuntu', sans-serif; letter-spacing: 1.5px">天赐の小站</p>
  <p style="text-align: center; word-spacing: 20px;">
    
  </p>
  <ul id="menu-new-1" class="menu">
    
      <li>
        <a href="/">
          <span class="faa-parent animated-hover">
            <i class="fa  fa-fort-awesome faa-horizontal" aria-hidden="true"></i>
            首页
          </span>
        </a>
        
      </li>
    
      <li>
        <a href="/archives">
          <span class="faa-parent animated-hover">
            <i class="fa  fa-terminal faa-vertical" aria-hidden="true"></i>
            极客
          </span>
        </a>
        
          <ul class="sub-menu">
            
              <li>
                <a href="/categories/技术/">
                  <i class="fa fa-code" aria-hidden="true"></i>
                  技术
                </a>
              </li>
            
              <li>
                <a href="/categories/Linux/">
                  <i class="fa fa-linux" aria-hidden="true"></i>
                  Linux
                </a>
              </li>
            
          </ul>
        
      </li>
    
      <li>
        <a href="/archives">
          <span class="faa-parent animated-hover">
            <i class="fa  fa-slack faa-spin" aria-hidden="true"></i>
            爱好
          </span>
        </a>
        
          <ul class="sub-menu">
            
              <li>
                <a href="/categories/电影/">
                  <i class="fa fa-film" aria-hidden="true"></i>
                  电影
                </a>
              </li>
            
              <li>
                <a href="/categories/日语/">
                  <i class="fa fa-language" aria-hidden="true"></i>
                  日语
                </a>
              </li>
            
          </ul>
        
      </li>
    
      <li>
        <a href="/archives">
          <span class="faa-parent animated-hover">
            <i class="fa  fa-archive faa-ring" aria-hidden="true"></i>
            归档
          </span>
        </a>
        
          <ul class="sub-menu">
            
              <li>
                <a href="/categories/生活/">
                  <i class="fa fa-file-text-o" aria-hidden="true"></i>
                  生活
                </a>
              </li>
            
              <li>
                <a href="/categories/随想/">
                  <i class="fa fa-commenting-o" aria-hidden="true"></i>
                  随想
                </a>
              </li>
            
              <li>
                <a href="/categories/资源/">
                  <i class="fa fa-cloud-download" aria-hidden="true"></i>
                  资源
                </a>
              </li>
            
              <li>
                <a href="/categories/转载/">
                  <i class="fa fa-book" aria-hidden="true"></i>
                  转载
                </a>
              </li>
            
          </ul>
        
      </li>
    
      <li>
        <a href="/comment/">
          <span class="faa-parent animated-hover">
            <i class="fa  fa-pencil-square-o faa-tada" aria-hidden="true"></i>
            留言板
          </span>
        </a>
        
      </li>
    
      <li>
        <a href="/links/">
          <span class="faa-parent animated-hover">
            <i class="fa  fa-link faa-shake" aria-hidden="true"></i>
            友人帐
          </span>
        </a>
        
      </li>
    
      <li>
        <a href="/donate/">
          <span class="faa-parent animated-hover">
            <i class="fa  fa-heart faa-burst" aria-hidden="true"></i>
            赞赏
          </span>
        </a>
        
      </li>
    
      <li>
        <a href="/">
          <span class="faa-parent animated-hover">
            <i class="fa  fa-leaf faa-falling" aria-hidden="true"></i>
            关于
          </span>
        </a>
        
          <ul class="sub-menu">
            
              <li>
                <a href="/about/">
                  <i class="fa fa-meetup" aria-hidden="true"></i>
                  我？
                </a>
              </li>
            
              <li>
                <a href="/theme-sakura/">
                  <i class="fa iconfont icon-sakura" aria-hidden="true"></i>
                  主题
                </a>
              </li>
            
          </ul>
        
      </li>
    
      <li>
        <a href="/atom.xml">
          <span class="faa-parent animated-hover">
            <i class="fa  fa-rss faa-passing" aria-hidden="true"></i>
            RSS
          </span>
        </a>
        
      </li>
    
  </ul>
  <p style="text-align: center; font-size: 13px; color: #b9b9b9;">&copy 2019 hexo-sakura</p>
</div>
<button onclick="topFunction()" class="mobile-cd-top" id="moblieGoTop" title="Go to top" style="display: none;"><i class="fa fa-chevron-up" aria-hidden="true"></i></button>
  <!--<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.css">-->
<!--<script src="https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.js"></script>-->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/css/aplayer/APlayer.min.css">
<script src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/js/aplayer/APlayer.min.js"></script>

<style>
  .aplayer .aplayer-lrc {
    height: 35px;
  }
  .aplayer .aplayer-lrc p{
    font-size: 16px;
    font-weight: 700;
    line-height: 18px !important;
  }
  .aplayer .aplayer-lrc p.aplayer-lrc-current{
    color: #FF1493;
  }
  /*.aplayer.aplayer-narrow .aplayer-body{
    left: -66px !important;
  }*/
  .aplayer.aplayer-fixed .aplayer-lrc {
    display: none;
  }
  .aplayer .aplayer-lrc.aplayer-lrc-hide {
      display:none !important;
  }
  .aplayer.aplayer-fixed .lrc-show {
    display: block;
    background: rgba(255, 255, 255, 0.8);
  }
</style>
<div class="aplayer"

    data-id="7040682880"

    data-server="tencent"

    data-type="playlist"

    data-fixed="true"

    data-autoplay="false"

    data-loop="all"

    data-order="random"

    data-preload="auto"

    data-volume="0.5"

    data-mutex="true"

    data-listFolded="false"

</div>
<!-- Meting 一个支持各种音乐平台(腾讯、网易...)的音乐 API 框架 -->
<!--<script src="https://cdn.jsdelivr.net/npm/meting@2.0.1/dist/Meting.min.js"></script>-->
<script src="https://cdn.jsdelivr.net/gh/tiancixiong/cdn@sakura-theme/js/aplayer/Meting.min.js"></script>
<script>
  $(function(){
    $('body').on('click', '.aplayer', function(){
      if($('.aplayer-button').hasClass('aplayer-play')) {
        $('.aplayer-lrc').removeClass('lrc-show');
      } else {
        $('.aplayer-lrc').addClass('lrc-show');
      }
    })
  });
</script>
</body>
</html>